/*********************************************************************************
 **      Copyright:  (C) 2024 linuxer<linuxer@email.com>
 **                  All rights reserved.
 **
 **       Filename:  temperature.c
 **    Description:  This file 
 **
 **        Version:  1.0.0(17/03/24)
 **         Author:  linuxer <linuxer@email.com>
 **      ChangeLog:  1, Release initial version on "17/03/24 16:52:22"
 **                 
 **********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "humidity.h"

int get_humidity(float *humidity)
{
	pthread_t 		tid;
	int 			ret;

	printf("humidity thread id = %d\n", syscall(SYS_gettid));

	pthread_mutex_init(&mut, NULL);
	pthread_create(&tid, NULL, threadPro, NULL);

	ret = pthread_mutex_lock(&mut);	
	printf("1, ret = %d, lock = %d, count = %d, owner = %d\n", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);

	// 初始化随机数生成器的种子，通常使用当前时间  
	srand(time(NULL));
	 // 生成 [0,1) 范围内的随机浮点数  
	*humidity = (float)rand() / (float)RAND_MAX;
	*humidity = *humidity*100;
	
	pthread_mutex_unlock(&mut);
	printf("2, ret = %d, lock = %d, count = %d, owner = %d\n", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);
	pthread_join(tid, NULL);
	pthread_mutex_destroy(&mut);

	
	return 0;
}

void *threadPro(void *arg)
{
	printf("child thread id = %d, wait to get mutex....\n", syscall(SYS_gettid));
	int ret = pthread_mutex_lock(&mut);
	printf("in child thread, ret = %d, lock = %d, count = %d, owner = %d\n", ret, mut.__data.__lock, mut.__data.__count, mut.__data.__owner);
}
